package cn.tedu.mall.search.test;

import java.util.Arrays;

public class QuickSort {
    public static void main(String[] args) {
        int[] a = {21, 31, 23, 8, 12, 3, 81, 20, 939, 19, 23, 91, 9, 2};
        System.out.println(Arrays.toString(a));
        sort(a);
        System.out.println(Arrays.toString(a));
    }

    private static void sort(int[] a) {
        sort(a, 0, a.length - 1);
    }

    private static void sort(int[] a, int left, int right) {
        if (left >= right) {
            return;
        }
        int base = a[left];
        int i = left;
        int j = right;
        while (i < j) {
            while (a[j] >= base && i < j) {
                j--;
            }
            a[i] = a[j];
            while (a[i] <= base && i < j){
                i++;
            }
            a[j] = a[i];
        }
        a[i] = base;
        sort(a,left,i-1);
        sort(a,i+1,right);
    }
}
